﻿namespace EFS.RC.ClientPlatform.UI
{
	/// Class MainWindow
	public abstract class MainWindow : System.Windows.Window
	{
		#region Composition

		// available session
		private EFS.RC.ClientPlatform.Session session = null;

		#endregion

		#region Delegates

		#endregion

		#region Constructors

		/// Implicit
		public MainWindow()
		{
		}

		#endregion

		#region Utilities

		/// <summary>
		/// ClosePlatform
		/// </summary>
		protected void ClosePlatform()
		{
			// stop setup
			EFS.RC.ClientPlatform.UI.Platform.Instance.BeginShutdown();
		}

		/// Used to show a message cross thread
		protected void ShowMessage(System.String text, System.String caption, System.Windows.MessageBoxButton button, System.Windows.MessageBoxImage icon)
		{
			// test & show
			if (!EFS.Common.UI.Shared.WindowHelper.IsDisposed(this))
			{
				// message to user
				System.Windows.MessageBox.Show(this, text, caption, button, icon);
			}
		}

		#endregion

		#region Window events

		/// Initialized
		protected override void  OnInitialized(System.EventArgs e)
		{
 			base.OnInitialized(e);
			
			// register for events
			EFS.RC.ClientPlatform.UI.Platform.Instance.OnTerminatedPlatform += OnPlatformTerminated;
			EFS.RC.ClientPlatform.UI.Platform.Instance.OnEstablishedSession += OnEstablishedSession;

			// start setup
			EFS.RC.ClientPlatform.UI.Platform.Instance.BeginStartup();
		}


		/// <summary>
		/// Handle Closing
		/// </summary>
		/// <param name="e"></param>
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			base.OnClosing(e);

			// check if we closed the platform already
			if (EFS.RC.ClientPlatform.UI.Platform.Instance.Status != EFS.RC.ClientPlatform.UI.Platform.PlatformStatus.Terminated)
			{
				// cancel
				e.Cancel = true;

				// close platform
				ClosePlatform();
			}
		}

		#endregion

		#region Messages & Session

		/// <summary>
		/// Platform terminated
		/// </summary>
		private void OnPlatformTerminated()
		{
			// hook in update
			this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Action(() =>
			{
				// remove events
				EFS.RC.ClientPlatform.UI.Platform.Instance.OnTerminatedPlatform -= OnPlatformTerminated;
				EFS.RC.ClientPlatform.UI.Platform.Instance.OnEstablishedSession -= OnEstablishedSession;

				// platform is terminated, no need to stay arround
				System.Windows.Application.Current.Shutdown();
			}));
		}

		// OnEstablishedSession
		private void OnEstablishedSession(EFS.RC.ClientPlatform.Session session)
		{
			// hook in update
			this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Action(() =>
			{
				// set session
				this.session = session;

				// message handler
				session.OnMessageReceived += ProcessUCMAMessage;
				session.OnTerminated += OnTerminatedSession;
			}));
		}

		// established
		private void OnTerminatedSession()
		{
			// hook in update
			this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Action(() =>
			{
				// check
				if (null != session)
				{
					// message handler
					session.OnMessageReceived -= ProcessUCMAMessage;
					session.OnTerminated -= OnTerminatedSession;

					// set session
					session = null;

					// clear tabs
					SessionTerminatedCleanup();
				}
			}));
		}

		/// <summary>
		/// Called by session terminated event
		/// </summary>
		protected abstract void SessionTerminatedCleanup();
	
		/// Process Message entry point
		protected void SendUCMAMessage(EFS.RC.CC.Messages.Message message)
		{
			// check session
			if (null != session)
			{
				// send
				session.SendMessage(message);
			}
		}

		/// Process Message entry point
		protected abstract void ProcessUCMAMessage(EFS.RC.CC.Messages.Message message);

		#endregion
	}

}